/*
 *     This file is part of Mobile GPS Logger.
 * 
 *     Mobile GPS Logger is free software: you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation, either version 3 of the
 *     License, or (at your option) any later version.
 * 
 *     Mobile GPS Logger is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License v3 for more details.
 * 
 *     You should have received a copy of the GNU General Public License v3
 *     along with Mobile GPS Logger. If not, see <http://www.gnu.org/licenses/>.
*/


package cx.ath.skyflyer.contactless;

import javax.microedition.contactless.ContactlessException;
import javax.microedition.contactless.DiscoveryManager;
import javax.microedition.contactless.ndef.NDEFMessage;
import javax.microedition.contactless.ndef.NDEFRecord;
import javax.microedition.contactless.ndef.NDEFRecordListener;
import javax.microedition.contactless.ndef.NDEFRecordType;
import javax.microedition.io.PushRegistry;

/**
 *
 * @author Ahti Legonkov
 */
final class RealNdefClient {
    public static void register(final RecordListener recordListener) {
        try {
            DiscoveryManager dm = DiscoveryManager.getInstance();
            NDEFRecordListener ndefListener = new NDEFRecordListener() {

                public void recordDetected(NDEFMessage ndefMessage) {
                    int recordCount = ndefMessage.getRecords().length;

                    // Check if the message carries a payload. If it does, the payload is
                    // assumed to be a URL needed to connect to a bluetooth device and the
                    // device and service discovery pages are skipped.
                    for (int i = 0; i < recordCount; ++i) {
                        NDEFRecord rec = ndefMessage.getRecord(i);
                        byte[] payload = rec.getPayload();
                        if (payload == null)
                            continue;

                        recordListener.recordDetected(new String(payload));
                        break;
                    }
                }
            };
            dm.addNDEFRecordListener(ndefListener, new NDEFRecordType(
                    NDEFRecordType.EXTERNAL_RTD, "urn:nfc:ext:skyflyer.ath.cx:gpslog"));
        } catch (ContactlessException e) {
            // Catch ContactlessException
        }

        // Make list of connections that are already registered
        String[] regConns = PushRegistry.listConnections(false);

        // Boolean to tell if wanted TargetType is already registered
        boolean registered = false;

        // Go trough list to see if wanted TargetType is already registered
        for (int i = 0; i < regConns.length; i++) {
            if (regConns[i].equals("ndef:external_rtd?name=urn:nfc:ext:skyflyer.ath.cx:gpslog")) {
                registered = true;
                break;
            }
        }

        if (!registered) {
            // not yet registered must do it.
            // TODO:
        }
    }
}
